package com.skt.common_hr.approval.model
{
	import com.skt.common_hr.approval.model.vo.DeputyVO;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class DeputyProxy extends CommonProxy
	{
		public static const NAME:String = "DeputyProxy";
		
		public static const LOAD_DEPUTY_COMPLETE:String 	= "loadDeputyComplete";
		public static const LOAD_DEPUTY_FAILED:String 		= "loadDeputyFailed";
		public static const SAVE_DEPUTY_COMPLETE:String 	= "saveDeputyComplete";
		public static const SAVE_DEPUTY_FAILED:String 		= "saveDeputyFailed";
		public static const DELETE_DEPUTY_COMPLETE:String 	= "deleteDeputyComplete";
		public static const DELETE_DEPUTY_FAILED:String 	= "deleteDeputyFailed";
		public static const CHECK_DEPUTY_COMPLETE:String 	= "checkDeputyComplete";
		public static const CHECK_DEPUTY_FAILED:String 		= "checkDeputyFailed";
		
		
		public var deputyInfo:DeputyVO;

		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function DeputyProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		public function checkDeputy(pernr:String):void
		{
			var params:Object = new Object;
			params["I_WPERNR"] = pernr;
			
			loadData("Z_HR_0104", params, resultCheck, faultCheck);
		}
		
		public function loadDeputy(pernr:String):void
		{
			var params:Object = new Object;
			params["WPERNR"] = pernr;
			params["GBN"]  = "S";
			
			loadData("Z_HR_0101", params, resultLoad, faultLoad);
		}
		
		public function saveDeputy(pernr:String, deputyInfo:DeputyVO):void
		{
			var params:Object = new Object;
			params["WPERNR"] = pernr;
			params["GBN"]  = "U";
			
			var table:Array = new Array();
			table.push(encodeJSON(deputyInfo));
			
			var tables:Object = new Object;
			tables["ITAB1"] = table;
			
			saveData("Z_HR_0101", params, tables, resultSave, faultSave);
		}
		
		public function delDeputy(pernr:String):void
		{
			var params:Object = new Object;
			params["WPERNR"] = pernr;
			params["GBN"]  = "D";
			
			var tables:Object = new Object;
			
			saveData("Z_HR_0101", params, tables, resultDel, faultDel);
		}
		
		private function resultLoad(event:ResultEvent):void
		{
			if(retCode==0) {
				deputyInfo = parseDeputy(event.result.ITAB1);
				facade.sendNotification(LOAD_DEPUTY_COMPLETE, this);
			}
			else {
				facade.sendNotification(LOAD_DEPUTY_FAILED, this);
			}
		}
		
		private function faultLoad(event:FaultEvent):void
		{
			facade.sendNotification(LOAD_DEPUTY_FAILED, this);
		}
		
		private function resultSave(event:ResultEvent):void
		{
			if(retCode==0) {
				facade.sendNotification(SAVE_DEPUTY_COMPLETE, this);
			}
			else {
				facade.sendNotification(SAVE_DEPUTY_FAILED, this);
			}
		}
		
		private function faultSave(event:FaultEvent):void
		{
			facade.sendNotification(SAVE_DEPUTY_FAILED, this);
		}
		
		private function resultDel(event:ResultEvent):void
		{
			if(retCode==0) {
				facade.sendNotification(DELETE_DEPUTY_COMPLETE, this);
			}
			else {
				facade.sendNotification(DELETE_DEPUTY_FAILED, this);
			}
		}
		
		private function faultDel(event:FaultEvent):void
		{
			facade.sendNotification(DELETE_DEPUTY_FAILED, this);
		}

		private function resultCheck(event:ResultEvent):void
		{
			if(retCode==0 && event.result["O_CPFLAG"]=="X") {
				facade.sendNotification(CHECK_DEPUTY_COMPLETE, this);
			}
			else {
				facade.sendNotification(CHECK_DEPUTY_FAILED, this);
			}
		}
		
		private function faultCheck(event:FaultEvent):void
		{
			facade.sendNotification(CHECK_DEPUTY_FAILED, this);
		}
		
		
		private function parseDeputy(src:ArrayCollection):DeputyVO
		{
			for each(var rec:* in src) {
				var vo:DeputyVO = new DeputyVO;
				vo.BEGDA 		=  DateUtil.stringToDateTime(rec.BEGDA);
				vo.ENDDA 		=  DateUtil.stringToDateTime(rec.ENDDA);
				vo.CPERNR		= StringUtil.trimFront(rec.CPERNR, "0");
				vo.WPERNR		= StringUtil.trimFront(rec.WPERNR, "0");
				vo.CNAME		= rec.CNAME;
				vo.WNAME		= StringUtil.NVL(rec.WNAME);
				vo.TITLE		= rec.TITLE;
				vo.DIS_FLAG		= rec.DIS_FLAG;
				vo.CONTENT		= rec.CONTENT;
				return vo;
			}
			return null;
		}
		
		private function encodeJSON(vo:DeputyVO):Object {
			var ret:Object = new Object;
			ret.BEGDA = DateUtil.toBasicString(vo.BEGDA);
			ret.ENDDA = DateUtil.toBasicString(vo.ENDDA);
			ret.CPERNR = vo.CPERNR;
			ret.CNAME = vo.CNAME;
			ret.TITLE = vo.TITLE;
			ret.DIS_FLAG = vo.DIS_FLAG;
			ret.CONTENT = vo.CONTENT;
			return ret;
		}
	}
}